package com.blue.seata.common;

import java.io.Serializable;
import java.util.Objects;

/**
 * @author liulei, lei.liu@htouhui.com
 * @version 1.0
 */
public class JsonResult<T> implements Serializable {

    private static final long serialVersionUID = 1752571016357023678L;

    private int status;
    private String message;
    private T data;

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static JsonResult ok() {
        return ok(null);
    }

    public static <T> JsonResult ok(T data) {
        JsonResult result = new JsonResult();
        result.setStatus(200);
        result.setMessage("OK");
        if (data != null) {
            result.setData(data);
        }
        return result;
    }

    public static JsonResult failed(String message) {
        JsonResult result = new JsonResult();
        result.setStatus(500);
        result.setMessage(message);
        return result;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof JsonResult)) {
            return false;
        }
        JsonResult result = (JsonResult) o;
        return status == result.status &&
                Objects.equals(message, result.message) &&
                Objects.equals(data, result.data);
    }

    @Override
    public int hashCode() {
        return Objects.hash(status, message, data);
    }
}

